<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Combo Box Tests</title>
<script type="text/javascript" src="../../deviceTheme.js" data-dojo-config="mblThemeFiles: ['base']"></script>
<script type="text/javascript" src="../../../../dojo/dojo.js" data-dojo-config="async: true, parseOnLoad: true"></script>
<script language="JavaScript" type="text/javascript">
	var timeoutInterval = 1000;
	require([
		"dojo/_base/connect",
		"dojo/_base/sniff",
		"dojo/dom-construct", // dojo.place
		"dojo/dom-class", // dojo.hasClass
		"dojo/store/Memory", // dojo.store.Memory
		"dojo/ready", // dojo.ready
		"dijit/registry",  // dijit.byId
		"doh/runner",	//doh functions
		"dojox/mobile/ComboBox",
		"dojox/mobile",				// This is a mobile app.
		"dojox/mobile/View",		// This mobile app uses mobile view
		"dojox/mobile/compat",		// This mobile app supports running on desktop browsers
		"dojox/mobile/parser"		// This mobile app uses declarative programming with fast mobile parser
	], function(connect, has, domConst, domClass, Memory, ready, registry, runner, ComboBox){

		var FRUITS = new Memory({
				idProperty:"name",
				data:[
					{name:"Apple"},
					{name:"Banana"},
					{name:"Cherry"}
			]});

		function _createComboBoxDeclaratively(id) {
			return registry.byId(id);
		};
		
		function _createComboBoxProgrammatically(placeHolderId){
			var widget = new ComboBox({store:FRUITS, id:placeHolderId, value:'Apple'});
			runner.assertNotEqual(null, widget);
			domConst.place(widget.domNode, placeHolderId, "replace");
			widget.startup();
			return widget;
		};
		
		function _createComboBoxProgrammaticallyWithSourceNodeReference(id){
			var widget = new ComboBox({store:FRUITS, value:'Apple'}, id);
			widget.startup();
			return widget;
		};

		function _assertCorrectComboBox(comboBox){
			runner.assertNotEqual(null, comboBox);
			runner.assertEqual(FRUITS.data[0].name, comboBox.domNode.value, comboBox.toString());
			comboBox.set("value", FRUITS.data[1].name);
			runner.assertEqual(FRUITS.data[1].name, comboBox.domNode.value, comboBox.toString());
		};

		function _showView2(){
			var view1 = registry.byId("view1");
			view1.performTransition("view2", 1, "none");
		};

		ready(function(){
			runner.register("dojox.mobile.test.doh.ComboBoxTests", [
				{
					name: "testInView1",
					timeout: 4000,
					runTest: function(){

						var d = new runner.Deferred();
						var comboBox1 = _createComboBoxDeclaratively("view1-comboBox1");
						var comboBox2 = _createComboBoxProgrammatically("view1-comboBox2");
						var comboBox3 = _createComboBoxProgrammaticallyWithSourceNodeReference("view1-comboBox3");
						setTimeout(d.getTestCallback(function(){
							var comboBox1 = registry.byId("view1-comboBox1");
							var comboBox2 = registry.byId("view1-comboBox2");
							var comboBox3 = registry.byId("view1-comboBox3");
							_assertCorrectComboBox(comboBox1);
							_assertCorrectComboBox(comboBox2);
							_assertCorrectComboBox(comboBox3);
						}), timeoutInterval);
						return d;
					}
				},
				{
					name: "testInView2",
					timeout: 4000,
					runTest: function(){
						var d = new runner.Deferred();
						var comboBox1 = _createComboBoxDeclaratively("view2-comboBox1");
						var comboBox2 = _createComboBoxProgrammatically("view2-comboBox2");
						var comboBox3 = _createComboBoxProgrammaticallyWithSourceNodeReference("view2-comboBox3");
						
						_showView2();
						
						setTimeout(d.getTestCallback(function(){
							var comboBox1 = registry.byId("view2-comboBox1");
							var comboBox2 = registry.byId("view2-comboBox2");
							var comboBox3 = registry.byId("view2-comboBox3");
							_assertCorrectComboBox(comboBox1);
							_assertCorrectComboBox(comboBox2);
							_assertCorrectComboBox(comboBox3);
						}), timeoutInterval);
						return d;
					}
				}
			]);
			runner.run();
		});
	})
</script>
</head>
<body>
	<div id="view1" data-dojo-type="dojox.mobile.View" selected="true">
		<h1>View 1</h1>
		<datalist id="fruits">
			<select data-dojo-type="dijit.form.DataList" data-dojo-props="id:'fruits'">
				<option value="fruit1">Apple</option>
				<option value="fruit2">Banana</option>
				<option value="fruit3">Cherry</option>
			</select>
		</datalist>
		<input id="view1-comboBox1" type="text" data-dojo-type="dojox.mobile.ComboBox" data-dojo-props="value:'Apple', list:'fruits'"></input>
		<div id="view1-comboBox2"></div>
		<input id="view1-comboBox3" type="text"></input>
	</div>
	<div id="view2" data-dojo-type="dojox.mobile.View">
		<h1>View 2</h1>
		<select data-dojo-type="dijit.form.DataList" data-dojo-props="id:'fruits2'">
			<option>Apple</option>
			<option>Banana</option>
			<option>Cherry</option>
		</select>
		<input id="view2-comboBox1" type="text" data-dojo-type="dojox.mobile.ComboBox" data-dojo-props="value:'Apple', list:'fruits2'"></input>
		<div id="view2-comboBox2"></div>
		<input id="view2-comboBox3" type="text"></input>
	</div>
</body>
</html>
